package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyzy.mapper.CommentLikeRecordMapper;
import com.cyzy.mapper.CommunityMapper;
import com.cyzy.mapper.CourseClaMapper;
import com.cyzy.mapper.MessageMapper;
import com.cyzy.pojo.*;
import com.cyzy.service.CommunityService;
import com.cyzy.vo.CommunityComment;
import com.cyzy.vo.PublishCommon;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Comment> implements CommunityService  {
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private CommentLikeRecordMapper commentLikeRecordMapper;

    @Autowired
    private MessageMapper messageMapper;



    @Override
    public List<CommunityComment> queryCurrentCommunityInfo(Long campusId, Long parentId, Long teacherId,Long subIndex) {
        System.out.println("家长查看");
        if (subIndex>0){
            System.out.println("查看老师社区");
            return communityMapper.queryTeacherCommunityInfoByParent(campusId,teacherId);
        }else{
            System.out.println("查看家长社区");
            return communityMapper.queryParentCommunityInfoByParent(campusId, parentId);
        }

    }

    @Override
    public List<Comment> selectCommentsBymesId(Long mesId) {
        return communityMapper.selectCommentsByMesId(mesId);
    }

    @Override
    public int insertCommentByMesId(Comment comment) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        comment.setRemarksTime(dateFormat.format(date));
        comment.setComStatus(1);
        return communityMapper.insert(comment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateUserCommonLike(CommentLikeRecord record) {
        try {
            if (record.getParentId()!=0&&record.getTeacherId()==0){
                System.out.println("家长点赞");
                //判断这个点赞记录是否存在（家长ID，动态ID，校区ID）
                return commentLikeMethod(record,"parentId",record.getParentId());

            }else if (record.getParentId()==0&&record.getTeacherId()!=0){
                System.out.println("老师点赞");
                return commentLikeMethod(record,"teacherId",record.getTeacherId());
            }else{
                System.out.println("社区点赞有误！！！");
            }
            return 0;
        }catch (Exception e){
            throw e;
        }
    }

    @Override
    public List<PublishCommon> queryUserListById(Long campusId, Long parentId, Long teacherId) {
        if (parentId!=0&&teacherId==0){
            System.out.println("家长查看孩子列表");
            return communityMapper.queryChildInfo(parentId);

        } else if (teacherId!=0&&parentId==0) {
            System.out.println("老师查看学生列表");
            return communityMapper.queryStudentInfo(teacherId);
        }else{
            System.out.println("列表查询失败");
        }
        return List.of();
    }

    @Override
    public int acceptTheDynamics(Message message) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        message.setDemo1("1");
        message.setMesSubmitTime(dateFormat.format(date));
        return messageMapper.insert(message);
    }

    //封装点赞的方法
    private int commentLikeMethod(CommentLikeRecord  record,String type,Long id){
        QueryWrapper<CommentLikeRecord> wrapper = new QueryWrapper<>();
        wrapper.eq(type,id)
                .eq("mesId",record.getMesId())
                .eq("campusId",record.getCampusId());
        CommentLikeRecord selectOne = commentLikeRecordMapper.selectOne(wrapper);
        System.out.println("selectOne的值为:"+selectOne);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();

        if (selectOne!=null){
            //存在，修改
            selectOne.setLikeTime(dateFormat.format(date));
            System.out.println("存在，修改");
            UpdateWrapper<CommentLikeRecord> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("comRecordId",selectOne.getComRecordId());
            updateWrapper.set("likeTime",selectOne.getLikeTime());
            int status;
            if (selectOne.getComLikeStatus()==1){
                status=0;
            }else {
                status=1;
            }
            updateWrapper.set("comLikeStatus",status);
            return commentLikeRecordMapper.update(null,updateWrapper);
        }else{
            //不存在，新增
            System.out.println("不存在，新增");
            record.setLikeTime(dateFormat.format(date));
            record.setComLikeStatus(1);
            return commentLikeRecordMapper.insert(record);
        }
    }
}
